package com.ruoyi.client.controller;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.client.service.ClientUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supply.domain.TMember;
import com.ruoyi.supply.enums.MemberConsumeTypeEnum;
import com.ruoyi.supply.enums.MemberGradeEnum;
import com.ruoyi.supply.service.impl.TMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author huangxiong
 * @date 2025/06/03 20:42
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/client/user")
@Api(tags = "客户端用户支付模块")
@Slf4j
public class ClientUserPayController {

    public final ClientUserService clientUserService;

    public final TMemberService tMemberService;


    /**
     * 小程序支付接口
     */
    @PostMapping("/buy/info/location/{id}")
    @ApiOperation("购买地点信息")
    public AjaxResult buyLocation(@PathVariable("id") Long infoId) {
        return AjaxResult.success(clientUserService.buyInfo(MemberConsumeTypeEnum.LOCATION, infoId));
    }

    /**
     * 小程序支付接口
     */
    @PostMapping("/buy/info/supply/{id}")
    @ApiOperation("购买货源信息")
    public AjaxResult buySupply(@PathVariable("id") Long infoId) {
        return AjaxResult.success(clientUserService.buyInfo(MemberConsumeTypeEnum.SOURCE, infoId));
    }


    @PostMapping("/buy/member/{grade}")
    public AjaxResult buyGrade(@PathVariable("grade") String grade){
        MemberGradeEnum memberGradeEnum = MemberGradeEnum.getByCode(grade);
        return AjaxResult.success(clientUserService.buyGrade(memberGradeEnum));
    }

    @GetMapping("/check/payStatus/{orderNo}")
    public AjaxResult checkOrderPayStatus(@PathVariable("orderNo") String orderNo) throws WxPayException {
        return AjaxResult.success("请求成功",clientUserService.checkOrderPayStatus(orderNo));
    }

    /**
     * 支付回调接口
     */
    @PostMapping("/notify")
    @Anonymous
    public String payNotify(@RequestBody String data, HttpServletRequest request) {
        return clientUserService.processNotify(data,request);
    }

}

